package com.ljk.study8httpfunction.controller;

import com.ljk.study8httpfunction.entity.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * desc:入口controller
 *
 * @author ljk
 * @date 2021/3/28 22:25
 */
@RestController
@Slf4j
public class HelloController {

    @RequestMapping(value = "/h1", method = {RequestMethod.GET, RequestMethod.POST})
    public Person h1(String name) {
        name = StringUtils.isEmpty(name) ? "未知姓名" : name;
        Person person = new Person("111", 22);
        return person;
    }


    @RequestMapping(value = "/hello")
    public String hello(String name, HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        name = StringUtils.isEmpty(name) ? "未知姓名" : name;
        Iterator<String> its = request.getHeaderNames().asIterator();
        its.forEachRemaining(i->{
            sb.append(String.format("[请求header]:%s,%s\r\n",i,request.getHeader(i)));
        });
        return sb.append(String.format("[请求返回]:hello,%s.你当前采用的是%s方法进行的请求", name, request.getMethod())).toString();
    }

    @RequestMapping(value = "/helloPerson")
    public String helloPerson(@RequestBody Person person, String remark, HttpServletRequest request) {
        String method = request.getMethod();
        return String.format("hello,%s岁的%s.你当前采用的是%s方法进行的请求,请求的备注是%s", person.getAge(), person.getName(), method, remark);
    }

    @RequestMapping(value = "/h2", method = {RequestMethod.GET, RequestMethod.POST})
    public Person h2(String name) throws InterruptedException {
        if (StringUtils.isEmpty(name)) {
            name = "未知姓名";
        }
        Person person = new Person(name, 22);
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Thread.sleep(100L * 1000 * 1000);
        return person;
    }
}
